package com.xzx.framework.util.http;


import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;


/**
 * 设置长连接策略，根据服务器的约束或者客户端的约束来设置长连接的时长；
 * 
 * @author wei
 * @time 2015年11月1日 下午8:41:57
 */
public class ConnectionKeepAliveStrategy extends DefaultConnectionKeepAliveStrategy {
	private static ConnectionKeepAliveStrategy instance = null;
	
	private ConnectionKeepAliveStrategy(){}
	
	public static ConnectionKeepAliveStrategy getInstance(){
		if(instance == null) {
			synchronized (ConnectionKeepAliveStrategy.class) {
				if(instance == null) {
					instance = new ConnectionKeepAliveStrategy();
				}
			}
		}
		
		return instance;
	}
	
	
	/**
	 * 服务器端配置（以tomcat为例）：keepAliveTimeout=60000，表示在60s内内，服务器会一直保持连接状态。
	 * 也就是说，如果客户端一直请求服务器，且间隔未超过60s，则该连接将一直保持，如果60s内未请求，则超时。
	 * 
	 * getKeepAliveDuration返回超时时间；
	 */
	@Override
	public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
		// 如果服务器指定了超时时间，则以服务器的超时时间为准
		HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
		while (it.hasNext()) {
			HeaderElement he = it.nextElement();
			String param = he.getName();
			String value = he.getValue();
			if (value != null && param.equalsIgnoreCase("timeout")) {
				try {
					System.out.println("服务器指定的超时时间：" + value + " 秒");
					return Long.parseLong(value) * 1000;
				} catch (NumberFormatException ignore) {
					ignore.printStackTrace();
				}
			}
		}
		
		long keepAlive = super.getKeepAliveDuration(response, context);
        
        // 如果服务器未指定超时时间，则客户端默认30s超时
		if (keepAlive == -1) {
			keepAlive = 30 * 1000;
		}
        
		return keepAlive;
	}

}
